﻿//-----------------------------------------------------------------------------+
// jQuery call AJAX Page Method                                                |
//-----------------------------------------------------------------------------+
function PageMethod(fn, paramArray, successFn, errorFn) {
    var pagePath = window.location.pathname;
    //-------------------------------------------------------------------------+
    // Create list of parameters in the form:                                  |
    // {"paramName1":"paramValue1","paramName2":"paramValue2"}                 |
    //-------------------------------------------------------------------------+
    var paramList = '';
    if (paramArray.length > 0) {
        for (var i = 0; i < paramArray.length; i += 2) {
            if (paramList.length > 0) paramList += ',';
            paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
        }
    }
    paramList = '{' + paramList + '}';
    //Call the page method
    $.ajax({
        type: "POST",
        url: pagePath + "/" + fn,
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: successFn,
        error: errorFn
    });
}
//-----------------------------------------------------------------------------+
// jQuery call AJAX WEB Method                                                 |
//-----------------------------------------------------------------------------+

function WebMethod(fn, paramArray, successFn, errorFn) {

    //-------------------------------------------------------------------------+
    // Create list of parameters in the form:                                  |
    // {"paramName1":"paramValue1","paramName2":"paramValue2"}                 |
    //-------------------------------------------------------------------------+
    var paramList = '';
    if (paramArray.length > 0) {
        for (var i = 0; i < paramArray.length; i += 2) {
            if (paramList.length > 0) paramList += ',';
            paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
        }
    }
    paramList = '{' + paramList + '}';

    //-------------------------------------------------------------------------+
    // Call the WEB method                                                     |
    //-------------------------------------------------------------------------+
    $.ajax({
        type: "POST",
        url: "MyWebService.asmx" + "/" + fn,
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: successFn,
        error: errorFn
    });
}
//---------------------------------------------+
// jQuery AJAX Call Succeeded.                 |
//---------------------------------------------+
function AjaxSucceeded(result) {
    alert(result.d);
}

//------------------------------------------------+
// jQuery AJAX Call FAILED.                       |
//------------------------------------------------+
function AjaxFailed(result) {
    alert("SERVICE Failed : " + result.d);
}

//------------------------------------------------+
// Client Side No Param Button Event Handler      |
//------------------------------------------------+
function ButtonPageTestNoParams_onclick() {

    PageMethod("TestNoParams", [], AjaxSucceeded, AjaxFailed);

}

//------------------------------------------------+
// Client Side WITH Params Button Event Handler   |         
//------------------------------------------------+
function ButtonPageTestWithParams_onclick() {

    PageMethod("TestWithParams", ["YourName", $("#TextBoxInName").val(),
                                      "YourZipCode", $("#TextBoxInZip").val()],
                                      AjaxSucceeded, AjaxFailed);
}
